#!/bin/bash

# 设置变量
BACKUP_DIR="/backup"
DATE=$(date +%Y%m%d_%H%M%S)
MYSQL_CONTAINER="worktool-mysql"
MYSQL_USER="root"
MYSQL_PASSWORD="your_password"
MYSQL_DATABASE="worktool"

# 创建备份目录
mkdir -p "${BACKUP_DIR}/${DATE}"

# 备份MySQL数据
echo "Backing up MySQL database..."
docker exec ${MYSQL_CONTAINER} mysqldump -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DATABASE} | gzip > "${BACKUP_DIR}/${DATE}/mysql_backup.sql.gz"

# 备份Redis数据
echo "Backing up Redis data..."
docker exec worktool-redis redis-cli SAVE
docker cp worktool-redis:/data/dump.rdb "${BACKUP_DIR}/${DATE}/redis_backup.rdb"

# 备份配置文件
echo "Backing up configuration files..."
cp -r config "${BACKUP_DIR}/${DATE}/"

# 压缩备份文件
cd "${BACKUP_DIR}"
tar czf "backup_${DATE}.tar.gz" "${DATE}"
rm -rf "${DATE}"

# 删除30天前的备份
find "${BACKUP_DIR}" -name "backup_*.tar.gz" -mtime +30 -delete

echo "Backup completed: ${BACKUP_DIR}/backup_${DATE}.tar.gz" 